package com.fh.shop.admin.controller;

import com.fh.shop.admin.common.ServerResponse;
import com.fh.shop.admin.util.OSSUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;

@Controller
@RequestMapping("file")
public class FileController {

    @Resource
    private HttpServletRequest request;

    private static final Logger LOGGER = LoggerFactory.getLogger(FileController.class);

    @ResponseBody
    @RequestMapping(value = "uploadImages",method = RequestMethod.POST)
    public ServerResponse uploadImages(MultipartFile[] imgFiles){
        StringBuilder paths = new StringBuilder();
        try {
            for (MultipartFile imgFile : imgFiles) {
                InputStream is = imgFile.getInputStream();
                //获取原文件名(目的是为了获取文件的后缀)
                String fileName = imgFile.getOriginalFilename();
                /*//将文件夹名的相对路径转换为绝对路径
                String realPath = request.getServletContext().getRealPath(SystemContant.FILE_IMAGE_PATH);
                //调用工具类的方法获得新的文件名
                String uploadFileName = FileUtil.copyFile(is, fileName, realPath);*/
                String filePath = OSSUtil.upload(is, fileName);
                paths.append(",").append(filePath);
            }
        } catch (IOException e) {
            e.printStackTrace();
            //手动打印异常信息
            LOGGER.error("图片上传——运行时异常："+e.getMessage());
            //手动抛出的运行时异常
            throw new RuntimeException(e);
        }
        return ServerResponse.success(paths.toString());
    }

    @ResponseBody
    @RequestMapping(value = "uploadImage",method = RequestMethod.POST)
    public ServerResponse uploadImage(MultipartFile imgFile){//属性名需要和前台name属性值对应
        //SpringMVC将上传的过程已经实现了，只不过文件路径、文件名、后缀不正确
        // 所以我们只需要修改文件名、文件的存放位置
        try {
            //获取文件流
            InputStream is = imgFile.getInputStream();
            //获取原文件名
            String fileName = imgFile.getOriginalFilename();
            /*//将文件夹名的相对路径转换为绝对路径
            String realPath = request.getServletContext().getRealPath(SystemContant.FILE_IMAGE_PATH);
            //调用工具类的方法获得新的文件名
            String uploadFileName = FileUtil.copyFile(is, fileName, realPath);*/
            String filePath = OSSUtil.upload(is, fileName);
            //返回文件的全路径 （文件夹名+文件名）
            return ServerResponse.success(filePath);
        } catch (IOException e) {
            //自带的打印的异常
            e.printStackTrace();
            //手动打印异常信息
            LOGGER.error("图片上传——运行时异常："+e.getMessage());
            //手动抛出的运行时异常
            throw new RuntimeException(e);
        }
    }

}
